package net.gaox.random.config.property;

import org.apache.commons.lang3.RandomUtils;

/**
 * <p> 随机选出一个端口 </p>
 *
 * @author gaox·Eric
 * @date 2020/5/22 00:09
 */
public class RandomServerPort {

    private volatile int serverPort;

    private final int start = 0;
    private final int end = 65535;

    public int nextValue(int start) {
        return nextValue(start, end);
    }

    public int nextValue(int start, int end) {
        start = Math.max(start, this.start);
        end = Math.min(end, this.end);

        if (serverPort == 0) {
            synchronized (this) {
                if (serverPort == 0) {
                    serverPort = RandomUtils.nextInt(start, end);
                }
            }
        }
        return serverPort;
    }

}
